package com.uzai.console.web.tb.tborder;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.component.excel.listener.ExcelListener;
import com.uzai.console.component.excel.template.TbPunishOrderExcelDemo;
import com.uzai.console.component.excel.template.TbRefundOrderExcelDemo;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.tb.tbcategory.TbCategoryQuery;
import com.uzai.console.dto.tb.tborder.*;
import com.uzai.console.dto.tb.tbpunishorder.TbPunishOrderExportResultDto;
import com.uzai.console.dto.tb.tbpunishorder.TbPunishOrderQuery;
import com.uzai.console.dto.tb.tbpunishorder.TbPunishOrderUpLoadDto;
import com.uzai.console.dto.tb.tbstoreblacklist.StoreBlacklistPrvInfoDto;
import com.uzai.console.dto.tb.tbtoken.TbTokenQuery;
import com.uzai.console.dto.user.UserQuery;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.device.DeviceTypeService;
import com.uzai.console.service.order.OrderReStatusService;
import com.uzai.console.service.order.OrderSourceService;
import com.uzai.console.service.order.OrderStatusService;
import com.uzai.console.service.tb.tborder.TbCategoryService;
import com.uzai.console.service.tb.tborder.TbOrderService;
import com.uzai.console.service.tb.tborder.TbOrderSyncService;
import com.uzai.console.service.tb.tborder.TbOrderTypeService;
import com.uzai.console.service.tb.tbstoreblacklist.StoreBlacklistPrvService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.exportinfo.ExportAmountVo;
import com.uzai.console.vo.order.OrderReStatusVo;
import com.uzai.console.vo.tb.tborder.*;
import com.uzai.console.vo.user.UserInfoVo;
import com.uzai.console.vo.wechatprivate.devicewechatinfo.DeviceWechatInfoSelectVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "淘宝订单",tags = "淘宝|淘宝订单")
@ApiSort(9)
@RestController
public class TbOrderController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(TbOrderController.class);

    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private TbCategoryService tbCategoryService;
    @Autowired
    private OrderStatusService orderStatusService;
    @Autowired
    private OrderReStatusService orderReStatusService;
    @Autowired
    private OrderSourceService orderSourceService;
    @Autowired
    private TbOrderTypeService tbOrderTypeService;
    @Autowired
    private StoreBlacklistPrvService storeBlacklistPrvService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private TbOrderSyncService tbOrderSyncService;

    @ApiOperation(value = "查询淘宝订单列表", notes = "查询淘宝订单列表", response = TbOrderInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/tbOrder/findTbOrderList")
    public Object findTbOrderList(@RequestBody TbOrderQuery tbOrderQuery){
        return tbOrderService.findTbOrderList(tbOrderQuery);
    }

    @ApiOperation(value = "查询淘宝订单详情", notes = "查询淘宝订单详情", response = TbOrderInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/tbOrder/findTbOrderInfo")
    public Object findTbOrderInfo(@RequestBody TbOrderDto tbOrderDto){
        return tbOrderService.findTbOrderInfo(tbOrderDto);
    }

    @ApiOperation(value = "查询淘宝订单统计信息(总收入、买家总金额、总利润、未绑定用户利润、未收货返利金额）", notes = "查询淘宝订单统计信息(总收入、买家总金额、总利润、未绑定用户利润、未收货返利金额）", response = TbOrderReportVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/tbOrder/findTbOrderReport")
    public Object findTbOrderReport(@RequestBody TbOrderQuery tbOrderQuery){
        return tbOrderService.findTbOrderRepot(tbOrderQuery);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "订单删除", notes = "订单删除")
    @ApiOperationSupport(order = 4)
    @PostMapping("/tbOrder/delTbOrder")
    public Object delTbOrder(@RequestBody TbOrderDelDto tbOrderDelDto){
        return tbOrderService.delTbOrder(tbOrderDelDto);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "确认收货", notes = "确认收货")
    @ApiOperationSupport(order = 5)
    @PostMapping("/tbOrder/confirmReceive")
    public Object confirmReceive(@RequestBody TbOrderDto tbOrderDto){
        return tbOrderService.confirmReceive(tbOrderDto);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "手动维权", notes = "手动维权")
    @ApiOperationSupport(order = 6)
    @PostMapping("/tbOrder/confirmRefund")
    public Object confirmRefund(@RequestBody TbOrderDto tbOrderDto){
        return tbOrderService.confirmRefund(tbOrderDto);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单冻结", notes = "订单冻结")
    @ApiOperationSupport(order = 7)
    @PostMapping("/tbOrder/confirmFrozen")
    public Object confirmFrozen(@RequestBody TbOrderDto tbOrderDto){
        return tbOrderService.confirmFrozen(tbOrderDto);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单解冻", notes = "订单解冻")
    @ApiOperationSupport(order = 8)
    @PostMapping("/tbOrder/confirmThaw")
    public Object confirmThaw(@RequestBody TbOrderDto tbOrderDto){
        return tbOrderService.confirmThaw(tbOrderDto);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "解绑用户", notes = "解绑用户")
    @ApiOperationSupport(order = 9)
    @PostMapping("/tbOrder/unbindUser")
    public Object unBindUser(@RequestBody TbOrderUnbindUserDto tbOrderUnbindUserDto){
        return tbOrderService.unbindUser(tbOrderUnbindUserDto);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "绑定用户", notes = "绑定用户")
    @ApiOperationSupport(order = 10)
    @PostMapping("/tbOrder/bindUser")
    public Object bindUser(@RequestBody TbOrderBindUserDto tbOrderBindUserDto){
        return tbOrderService.bindUser(tbOrderBindUserDto);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改解冻时间", notes = "修改解冻时间")
    @ApiOperationSupport(order = 11)
    @PostMapping("/tbOrder/updateThawTime")
    public Object updateThawTime(@RequestBody TbOrderThawTimeDto tbOrderThawTimeDto){
        return tbOrderService.updateThawTime(tbOrderThawTimeDto);
    }

    @ApiOperation(value = "查询联盟账号接口", notes = "查询联盟账号接口", response = TbOrderTokenVo.class)
    @ApiOperationSupport(order = 12)
    @PostMapping("/tbOrder/findAlimmList")
    public Object findAlimmList(@RequestBody TbTokenQuery tbTokenQuery){
        tbTokenQuery.setType(0);//淘宝授权
        return tbOrderService.findTbTokenList(tbTokenQuery);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.DOWNLOAD)
    @ApiOperation(value = "手动下载淘宝订单", notes = "手动下载淘宝订单")
    @ApiOperationSupport(order = 13)
    @PostMapping("/tbOrder/downloadOrder")
    public Object downloadOrder(@RequestBody TbOrderDownLoadDto tbOrderDownLoadDto){
        //tbOrderService.downloadOrder(tbOrderDownLoadDto);
        //修改成通过api拉取，不入订单库，直接下载订单同步到业务库
        tbOrderService.downloadOrder_notInStock(tbOrderDownLoadDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.DOWNLOAD)
    @ApiOperation(value = "订单刷新", notes = "订单刷新")
    @ApiOperationSupport(order = 14)
    @PostMapping("/tbOrder/refreshOrder")
    public Object refreshOrder(@RequestBody TbOrderDto tbOrderDto){
        tbOrderService.refreshOrder(tbOrderDto);
        return true;
    }

    @ApiOperation(value = "查询所有未绑定用户订单", notes = "查询所有未绑定用户订单")
    @ApiOperationSupport(order = 15)
    @PostMapping("/tbOrder/findUnbindUserList")
    public Object findUnbindUserList(@RequestBody TbOrderUnbindDtoQuery tbOrderUnbindDtoQuery){
        return tbOrderService.findUnbindUserList(tbOrderUnbindDtoQuery);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除所有未绑定用户订单", notes = "删除所有未绑定用户订单")
    @ApiOperationSupport(order = 16)
    @PostMapping("/tbOrder/deleteUnbindUserList")
    public Object deleteUnbindUserList(@RequestBody TbOrderDeleteUnbindDto tbOrderDeleteUnbindDto){
        return tbOrderService.deleteUnbindUserList(tbOrderDeleteUnbindDto);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "更新7天前未收货订单", notes = "更新7天前未收货订单")
    @ApiOperationSupport(order = 17)
    @PostMapping("/tbOrder/updateTbOrderUnReceived")
    public Object updateTbOrderUnReceived(){
        tbOrderService.updateTbOrderUnReceivedList(getLoginInfo().getMerId());
        return true;
    }

    @ApiOperation(value = "查询淘宝类目列表", notes = "查询淘宝类目列表", response = TbCategoryVo.class)
    @ApiOperationSupport(order = 18)
    @PostMapping("/tbcategory/all")
    public Object tbcategoryAll(@RequestBody TbCategoryQuery tbCategoryQuery) {
        return tbCategoryService.findTbCategoryList(tbCategoryQuery);
    }

    @ApiOperation(value = "查询淘宝订单状态列表", notes = "查询淘宝订单状态列表", response = TbCategoryVo.class)
    @ApiOperationSupport(order = 19)
    @PostMapping("/tbOrder/getOrderStatusList")
    public Object getOrderStatusList() {
        return orderStatusService.findTbOrderStatusList();
    }

    @ApiOperation(value = "查询淘宝订单返利状态列表", notes = "查询淘宝订单返利状态列表", response = OrderReStatusVo.class)
    @ApiOperationSupport(order = 20)
    @PostMapping("/tbOrder/getOrderReStatusList")
    public Object getOrderReStatusList() {
        return orderReStatusService.findOrderReStatusList();
    }

    @ApiOperation(value = "查询淘宝订单分类列表", notes = "查询淘宝订单分类列表", response = TbOrderTypeVo.class)
    @ApiOperationSupport(order = 21)
    @PostMapping("/tbOrder/getOrderTypeList")
    public Object getOrderTypeList() {
        return tbOrderTypeService.findTbOrderTypeList();
    }

    @ApiOperation(value = "查询淘宝订单来源列表", notes = "查询淘宝订单订单来源列表", response = OrderReStatusVo.class)
    @ApiOperationSupport(order = 22)
    @PostMapping("/tbOrder/getOrderSourceList")
    public Object getOrderSourceList() {
        return orderSourceService.findOrderSourceList();
    }

    @ApiOperation(value = "查询所有未绑定用户统计信息(总收入、买家总金额、总利润）", notes = "查询所有未绑定用户订单统计信息(总收入、买家总金额、总利润）", response = TbOrderReportVo.class)
    @ApiOperationSupport(order = 23)
    @PostMapping("/tbOrder/findUnbindUserReport")
    public Object findUnbindUserReport(@RequestBody TbOrderUnbindDtoQuery tbOrderUnbindDtoQuery){
        return tbOrderService.findUnbindUserReport(tbOrderUnbindDtoQuery);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加店铺黑名单", notes = "添加店铺黑名单")
    @ApiOperationSupport(order = 24)
    @PostMapping("/tbOrder/addStoreBlacklistPrv")
    public Object addStoreBlacklistPrv(@RequestBody StoreBlacklistPrvInfoDto storeBlacklistPrvInfoDto){
        storeBlacklistPrvService.addStoreBlacklistPrv(storeBlacklistPrvInfoDto);
        return true;
    }

    @ApiOperation(value = "分页查询会员信息列表", notes = "分页查询会员信息列表", response = UserInfoVo.class)
    @ApiOperationSupport(order = 25)
    @PostMapping("/tbOrder/findUserPage")
    public Object findUserPage(@RequestBody UserQuery userQuery) {
        return userService.findUserPage(userQuery);
    }

    @ApiOperation(value = "查询设备类型下拉列表", notes = "查询设备类型下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 26)
    @PostMapping("/tbOrder/findDeiviceTypeListSelect")
    public Object findDeiviceTypeListSelect() {
        return deviceTypeService.findDeviceTypeListSelect();
    }


    @ApiOperation(value = "查询机器人下拉列表", notes = "查询机器人下拉列表", response = DeviceInfoVo.class)
    @ApiOperationSupport(order = 27)
    @PostMapping("/tbOrder/findDeviceWechatInfoSelectList")
    public Object findDeviceWechatInfoSelectList() {
        DeviceQuery deviceQuery = new DeviceQuery();
        deviceQuery.setMerId(getLoginInfo().getMerId());
        return deviceService.findDeviceList(deviceQuery);
    }

    @ApiOperation(value = "上传维权订单文件", notes = "上传维权订单文件")
    @ApiOperationSupport(order = 28)
    @PostMapping("/tbOrder/uploadTbreFundOrderFile")
    public Object uploadTbreFundOrderFile(MultipartFile file){

        if(file == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "文件不存在");
        }

        try {

            ExcelTypeEnum excelTypeEnum = ExcelTypeEnum.XLSX;

            if(file.getOriginalFilename().endsWith(".csv")){
                excelTypeEnum = ExcelTypeEnum.CSV;
            }else if(file.getOriginalFilename().endsWith(".xls")){
                excelTypeEnum = ExcelTypeEnum.XLS;
            }else{
                excelTypeEnum = ExcelTypeEnum.XLSX;
            }

            EasyExcel.read(file.getInputStream(), TbRefundOrderExcelDemo.class, new ExcelListener(p->{
                //保存逻辑（可封装一个普通的sevice）p-为我们执行函数时传入的list
                tbOrderService.uploadTbPunishOrderFile(p);
            })).excelType(excelTypeEnum).sheet().doRead();
        } catch (IOException e) {
            logger.error("上传处罚订单失败:error={}", e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "上传失败");
        }
        return true;
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "追回返利", notes = "追回返利")
    @ApiOperationSupport(order = 29)
    @PostMapping("/tbOrder/rebateRefound")
    public Object rebateRefound(@RequestBody TbOrderDto tbOrderDto){
        return tbOrderService.rebateRefound(tbOrderDto);
    }

    @ApiOperation(value = "淘宝订单查询结果导出文件前检验接口", notes = "淘宝订单查询结果导出文件前检验接口", response = ExportAmountVo.class)
    @ApiOperationSupport(order = 30)
    @PostMapping("/tbOrder/getExportAmount")
    public Object getExportAmount(@RequestBody TbOrderQuery tbOrderQuery){
        return tbOrderSyncService.getExportAmount(tbOrderQuery);
    }

    @SystemLogType(model = SystemModelEnum.TbOrder, type = OperationTypeEnum.EXPORT)
    @ApiOperation(value = "淘宝订单查询结果导出文件", notes = "淘宝订单查询结果导出文件")
    @ApiOperationSupport(order = 31)
    @PostMapping("/tbOrder/exportByQeuryResult")
    public Object exportByQeuryResult(@RequestBody TbOrderExportResultDto tbOrderExportResultDto){
        tbOrderService.exportByQeuryResult(tbOrderExportResultDto);
        return true;
    }

}


